#include <windows.h>
#include "option.h"
#include "../resource/resource.h"

INT_PTR CALLBACK JoinDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
            SendDlgItemMessage(hwndDlg, IDC_PLAYERNAME, WM_SETTEXT, 0, (LPARAM)GetOption("PlayerName"   ).c_str());
            SendDlgItemMessage(hwndDlg, IDC_IPADDRESS,  WM_SETTEXT, 0, (LPARAM)GetOption("ServerAddress").c_str());
            SendDlgItemMessage(hwndDlg, IDC_PORT,       WM_SETTEXT, 0, (LPARAM)GetOption("ServerPort"   ).c_str());
            return TRUE;

        case WM_COMMAND:
            switch (wParam)
            {
                case IDOK:
                {
                    char msg[256];

                    // Save the settings
                    SendDlgItemMessage(hwndDlg, IDC_PLAYERNAME, WM_GETTEXT, sizeof(msg), (LPARAM)msg);
                    SetOption("PlayerName", msg);

                    SendDlgItemMessage(hwndDlg, IDC_IPADDRESS,  WM_GETTEXT, sizeof(msg), (LPARAM)msg);
                    SetOption("ServerAddress", msg);

                    SendDlgItemMessage(hwndDlg, IDC_PORT,       WM_GETTEXT, sizeof(msg), (LPARAM)msg);
                    SetOption("ServerPort", msg);

                    // Start the game (client)
                    EndDialog(hwndDlg, 1);
                    return TRUE;
                }
                case IDCANCEL:
                    // Back to main menu
                    EndDialog(hwndDlg, IDD_MAIN);
                    return TRUE;
            }
        break;
    }

    return FALSE;
}
